package com.example.music08.fragment;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import androidx.fragment.app.Fragment;

import com.example.music08.MainActivity2;
import com.example.music08.R;
import com.example.music08.adapter.MusicAdapter;
import com.example.music08.entity.Music;
import com.example.music08.utils.MusicUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * 获取本地音乐--实现本地音乐扫描功能，自动刷新列表   内容解析者
 */
public class BlankFragment2 extends Fragment {


    private EditText etLocal;
    private ListView lvLocal;
    ContentResolver resolver;

    List<Music.DataBean> list = new ArrayList<>();
    MusicAdapter musicAdapter;

    public BlankFragment2() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_blank2, container, false);
        initView(view);
        resolver = getActivity().getContentResolver();

        musicAdapter = new MusicAdapter(list);
        lvLocal.setAdapter(musicAdapter);
        /**
         * 获取音频 : (读sd卡的权限) -----------///////////---------------------
         *      * (1)uri:MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
         *      * (2)字段：TITLE：歌名   ARTIST 歌手   DURATION 总时长  DATA 地址  SIZE 大小
         *
         *  MediaStore.Audio.Media.TITLE,  歌名
         *  MediaStore.Audio.Media.ARTIST, 歌手
         *  MediaStore.Audio.Media.DURATION, 总时长
         *  MediaStore.Audio.Media.DATA, 地址
         *  MediaStore.Audio.Media.SIZE,  大小
         *  MediaStore.Audio.Media.ALBUM_ID, 专辑封面的id
         *  };
         *          "content://media/external/audio/albums/"+专辑封面的id;        专辑封面的uri
         *
         *  封面字段：album_art
         */
        cha();
        etLocal.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                cha();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        return view;
    }
    @SuppressLint("Range")
    private void cha() {
        list.clear();
        Cursor cursor;
        if(etLocal.getText().toString().isEmpty()){
            cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        }else{
            cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Audio.Media.TITLE+"=?", new String[]{etLocal.getText().toString()}, null);
        }
        if(cursor!=null){
            while (cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String singer = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                int duration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String album_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                //再次查询获取封面图片
                Cursor query = resolver.query(Uri.parse("content://media/external/audio/albums/" + album_id), null, null, null, null);
                if(query!=null){
                    while (query.moveToNext()){
                        String picUrl = query.getString(query.getColumnIndex("album_art"));
                        list.add(new Music.DataBean(0,name,singer,data,picUrl,duration,""));
                    }
                    query.close();
                }
            }
            cursor.close();
            MusicUtils.getInstance().setList(list);
            musicAdapter.notifyDataSetChanged();

        }
    }

    private void initView(View view) {
        etLocal = (EditText) view.findViewById(R.id.et_local);
        lvLocal = (ListView) view.findViewById(R.id.lv_local);
    }

    @Override
    public void onResume() {
        super.onResume();
        //执行查询操作
        cha();
        lvLocal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MusicUtils.getInstance().play(position);
                //MusicUtils.getInstance().setIndex(position);
                Intent intent = new Intent(getActivity(), MainActivity2.class);
                startActivity(intent);

            }
        });
    }
}